package czk.concrent.threadpool.method.practice;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 执行全部线程的练习
 *
 * @Author:ChenZhangKun
 * @Date: 2021/1/25 22:53
 */
public class InvokeAllDemo {
    public static void main(String[] args) throws InterruptedException {
        /**
         * 创建线程池
         */
        ExecutorService threadPool = Executors.newFixedThreadPool(4);
        List<Future<Object>> futures = threadPool.invokeAll(Arrays.asList(
                () -> {
                    System.out.println("执行第一个任务");
                    return 1;
                },
                () -> {
                    System.out.println("执行第二个任务");
                    return 2;
                }
        ));
        // 拿到全部返回内容
        futures.forEach(x->{
            try {
                System.out.println(x.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
}
